package com.neusoft.EYSystem;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;
@Component
public class AuthGatewayFilterFactory implements GlobalFilter {

	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		org.springframework.http.server.reactive.ServerHttpRequest request = exchange.getRequest();
		String requestPath = request.getURI().getPath();
		if(requestPath.indexOf("login")>-1) {
			return chain.filter(exchange);
		}
		if(requestPath.indexOf("upload")>-1) {
			return chain.filter(exchange);
		}
		if(requestPath.indexOf("captcha")>-1) {
			return chain.filter(exchange);
		}
		// TODO Auto-generated method stub
		String token=exchange.getRequest().getHeaders().getFirst("token");
		ServerHttpResponse response=exchange.getResponse();
		System.out.println("执行鉴权");
		if(token==null||!token.equals("admin")) {			
			response.setStatusCode(HttpStatus.UNAUTHORIZED);
			return response.setComplete();
		}
		
		return chain.filter(exchange);
	}

}
